package cn.yourkingda.service.impl;

import java.util.List;

import org.nutz.dao.Cnd;
import org.nutz.service.NameEntityService;
import cn.yourkingda.pojo.Customer;
import cn.yourkingda.service.CustomerService;
import cn.yourkingda.util.MyJson;

public class CustomerServiceImpl extends NameEntityService<Customer> implements CustomerService{

	
	/**
	 * 更新客户
	 */
	public MyJson updateCustomer(Customer customer) {
		String[] tips={"客户编号或者客户名字不能为空","客户编号已存在","更新成功","新增成功"};
		MyJson myJson = new MyJson();
		int status=0,msgNo=0;
		// TODO Auto-generated method stub
		if(customer.getCustomerno()==null||customer.getCustomername()==null){
			status=-1;
			msgNo=0;
		}
			if(customer.getId()>0){
				this.dao().update(customer);
				status=1;
				msgNo=2;
			}else{
				List customers = this.dao().query(Customer.class,Cnd.where("customerno","=",customer.getCustomerno()));
				if(customers.size()>0){
					status=-1;
					msgNo=1;
				}
				if(status!=-1){
				this.dao().insert(customer);
				status=1;
				msgNo=3;}
			}
		
		
		myJson.setStatus(status);
		myJson.setMsg(tips[msgNo]);
		return myJson;
		
	}

	

	public void getCustomer(Customer customer) {
		// TODO Auto-generated method stub
		this.dao().fetch(Customer.class,customer.getId());
	}
	/**
	 * 首页显示客户列表
	 */
	
	public List listCustomer() {
		// TODO Auto-generated method stub
		List list = this.dao().query(Customer.class,Cnd.where("id",">",0).desc("id"));
		return list;
	}


	/**
	 * 删除用户
	 */
	
	public int delCustomer(int id) {
		// TODO Auto-generated method stub
		int res = this.dao().delete(Customer.class, id);
		return res;
	}

}
